/**
 * @description 商品产品列表增删改查 api 路由
 * @author LI
 */

const router = require('koa-router')()
const {
  getProductAll,
  addProduct,
  updateProduct,
  deleteProduct
} = require('../../controller/product')

router.prefix('/jihuide/product')

// 添加商品产品信息
router.post('/addProduct', async (ctx, next) => {
  let {
    goodsInfoId,
    goodsPhotoId,
    productSn,
    productName,
    productNumber,
    productPrice,
    productUnit,
    photoCoordinate,
    photoDirect,
    productLink,
    productRemark,
  } = ctx.request.body
  goodsInfoId = parseInt(goodsInfoId)
  goodsPhotoId = parseInt(goodsPhotoId)
  productNumber = parseInt(productNumber)
  productPrice = Number(productPrice)
  // 接下来交给控制层
  ctx.body = await addProduct({
    goodsInfoId,
    goodsPhotoId,
    productSn,
    productName,
    productNumber,
    productPrice,
    productUnit,
    photoCoordinate,
    photoDirect,
    productLink,
    productRemark,
  })
})

// 根据图片 id 查询产品信息 或者名称，编号
router.post('/getProductAll', async (ctx, next) => {
  let { id, productSn, productName, page, limit } = ctx.request.body
  id = parseInt(id)
  // 接下来交给控制层
  ctx.body = await getProductAll({
    id,
    productSn,
    productName,
    page,
    limit,
  })
})

// 修改商品产品信息
router.post('/updateProduct', async (ctx, next) => {
  let {
    id,
    productName,
    productNumber,
    productPrice,
    productUnit,
    photoCoordinate,
    photoDirect,
    productLink,
    productRemark,
  } = ctx.request.body
  id = parseInt(id)
  productNumber = parseInt(productNumber)
  productPrice = Number(productPrice)
  // 接下来交给控制层
  ctx.body = await updateProduct({
    id,
    productName,
    productNumber,
    productPrice,
    productUnit,
    photoCoordinate,
    photoDirect,
    productLink,
    productRemark,
  })
})

// 删除商品信息
router.post('/deleteProduct', async (ctx, next) => {
  let { id } = ctx.request.body
  id = parseInt(id)
  // 接下来交给控制层
  ctx.body = await deleteProduct(id)
})

module.exports = router
